package com.yodawnla.easyPuyo.scene;

import org.anddev.andengine.entity.sprite.Sprite;

import android.view.KeyEvent;

import com.yodawnla.easyPuyo.HUD.CharacterStateHud;
import com.yodawnla.easyPuyo.system.Fonts;
import com.yodawnla.easyPuyo.system.Save;
import com.yodawnla.easyPuyo.system.Tex;
import com.yodawnla.easyPuyo.system.Values;
import com.yodawnla.easyPuyo.system.YS;
import com.yodawnla.lib.YoScene;
import com.yodawnla.lib.util.save.YoSaveFile;
import com.yodawnla.lib.util.save.YoSaveManager;
import com.yodawnla.lib.util.widget.YoButton;
import com.yodawnla.lib.util.widget.YoText;

public class TechTitleScene extends YoScene
{	 
	public static TechTitleScene getInstance()
	{
		if (instance == null)
			instance = new TechTitleScene();
		return instance;
	}
	
	static TechTitleScene instance;
	
	Boolean mRule01Complete=false;
	Boolean mRule02Complete=false;
	Boolean mTutorialComplete=false;
	YoButton mRule01;
	YoButton mRule02;
	YoButton mTutorial;
	YoButton mStage01;
	YoButton mStage02;
	YoButton mBackBtn;
	
	YoSaveFile mGameSaveFile = YoSaveManager.getInstance().getSaveFile(Save.FileName.Game);
	
	@Override
	public void loadScene()
	{ 
		Sprite bg= new Sprite(0, 0,480,800, getTexture(Tex.BaseBg));
		this.attachChild(bg);
		YoText learnRuleText = new YoText(0, 0, Fonts.White20, "Tutoril Learning Two Rules");
		this.attachChild(learnRuleText);
		learnRuleText.setPosition((480-learnRuleText.getWidth())/2, 114);	
		YoText rule01Text = new YoText(0, 0, Fonts.White20, "RULE01");
		this.attachChild(rule01Text);
		rule01Text.setPosition(77,165);
		YoText rule02Text = new YoText(0, 0, Fonts.White20, "RULE02");
		this.attachChild(rule02Text);
		rule02Text.setPosition(317,165);
		
		mBackBtn = new YoButton(this,400,33, getTexture(Tex.Back))
		{
			@Override
			public void onClickedEvent()
			{
				toScene(YS.StageScene);
			}
		};
		this.attachChild(mBackBtn);
		
		mRule01 = new YoButton(this, 24, 193, 204,313,getTexture(Tex.GaChaItemIcon))
		{
			@Override
			public void init()
			{
				YoText text = new YoText(0, 0, Fonts.White20, "Same number");
				this.attachChild(text);
				text.setPosition((this.getWidth()-text.getWidth())/2,244);
			}
			
			@Override
			public void onClickedEvent()
			{
				Tech1Scene.getInstance().setMod(Values.Mod.Count);
				toScene(YS.Tech1Scene);
			}
		}; 
		this.attachChild(mRule01);
		
		mRule02 = new YoButton(this,251, 198,204,313,getTexture(Tex.DiamondItemIcon))
		{
			@Override
			public void init()
			{
				YoText text = new YoText(0, 0, Fonts.White20, "Same Color");
				this.attachChild(text);
				text.setPosition((this.getWidth()-text.getWidth())/2, 244);
			}
			
			@Override
			public void onClickedEvent()
			{
				Tech1Scene.getInstance().setMod(Values.Mod.Type);
				toScene(YS.Tech1Scene);
			}
		}; 
		this.attachChild(mRule02);
		
		mTutorial = new YoButton(this,33, 10,getTexture(Tex.StageBtn))
		{
			@Override
			public void init()
			{
				YoText text = new YoText(97, 18, Fonts.White20, "Watch Tutorial");
				this.attachChild(text);
			}
		};
		this.attachChild(mTutorial);
		
		mTutorial.setPosition((480-mTutorial.getBaseWidth())/2, mTutorial.getY());
		mStage01 = new YoButton(this,34, 520,getTexture(Tex.StageBtn))
		{
			@Override
			public void init()
			{
				YoText text = new YoText(0, 0, Fonts.White20, "Real Combat");
				this.attachChild(text);
				text.setPosition((this.getWidth()-text.getWidth())/2,(this.getHeight()-text.getHeight())/2);
			}
			@Override
			public void onClickedEvent()
			{
				 toScene(YS.Tech2Scene);
			}
		};
		this.attachChild(mStage01);
		
		mStage01.setPosition((480-mStage01.getBaseWidth())/2, mStage01.getY());
		mStage01.setVisible(false);
//		mStage02 = new YoButton(this,32, 620,getTexture(Tex.StageBtn))
//		{
//			@Override
//			public void init()
//			{
//				YoText text = new YoText(0, 0, Fonts.White20, "Bounce Stage");
//				this.attachChild(text);
//				text.setPosition((this.getWidth()-text.getWidth())/2,(this.getHeight()-text.getHeight())/2);
//			}
//		};
//		this.attachChild(mStage02);
//		mStage02.setPosition((480-mStage02.getBaseWidth())/2, mStage02.getY());
		 

		mRule01Complete=mGameSaveFile.getBool(Save.Game.TutorialRule01Complete);
		mRule02Complete=mGameSaveFile.getBool(Save.Game.TutorialRule02Complete); 
		mTutorialComplete=mGameSaveFile.getBool(Save.Game.TutorialComplete);  
	}

	@Override
	public void onEnterScene()
	{
		CharacterStateHud.getInstance().hide();
		 
		if(mRule01Complete)
		{ 
			mRule01.setColor(0.5f,0.5f,0.5f); 
		}
		if(mRule02Complete)
			mRule02.setColor(0.5f,0.5f,0.5f); 
		
		if(mRule01Complete&&mRule02Complete&&mTutorialComplete==false)
		{
			mStage01.setVisible(true);
		}
		if(mTutorialComplete==true)
		{
			mStage01.setVisible(false);
		}
		 
	}

	@Override
	public void onExitScene()
	{	 
	}

	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent)
	{
		switch (pKeyCode)
		{
		case KeyEvent.KEYCODE_BACK:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{
				return false;
			}
			break;
		case KeyEvent.KEYCODE_MENU:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{
				return true;
			}
			break;
		}
		return false;
	}
}